package com.taotao.shop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.taotao.shop.common.util.PageUtils;
import com.taotao.shop.common.util.Query;
import com.taotao.shop.product.mapper.PmsBrandMapper;
import com.taotao.shop.product.mapper.PmsCategoryMapper;
import com.taotao.shop.product.pojo.entity.PmsBrand;
import com.taotao.shop.product.pojo.entity.PmsCategory;
import com.taotao.shop.product.pojo.entity.PmsCategoryBrandRelation;
import com.taotao.shop.product.mapper.PmsCategoryBrandRelationMapper;
import com.taotao.shop.product.service.PmsBrandService;
import com.taotao.shop.product.service.PmsCategoryBrandRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌分类关联 服务实现类
 * </p>
 *
 * @author zwr
 * @since 2021-06-14
 */
@Service
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements PmsCategoryBrandRelationService {


    @Autowired
    PmsBrandMapper brandMapper;
    @Autowired
    PmsCategoryBrandRelationMapper categoryBrandRelationMapper;

    @Autowired
    PmsCategoryMapper categoryMapper;

    @Autowired
    PmsBrandService brandService;
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        PmsCategoryBrandRelation relationEntity = new PmsCategoryBrandRelation();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,new UpdateWrapper<PmsCategoryBrandRelation>().eq("brand_id",brandId));
    }

    @Override
    public List<PmsBrand> getBrandsByCatId(Long catId) {

        List<PmsCategoryBrandRelation> catelogId = categoryBrandRelationMapper.selectList(new QueryWrapper<PmsCategoryBrandRelation>().eq("catelog_id", catId));
        List<PmsBrand> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            PmsBrand byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryBrandRelation> page = this.page(
                new Query<PmsCategoryBrandRelation>().getPage(params),
                new QueryWrapper<PmsCategoryBrandRelation>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(PmsCategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //1、查询详细名字
        PmsBrand brandEntity = brandMapper.selectById(brandId);
        PmsCategory categoryEntity = categoryMapper.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);

    }
}
